package gemini.basic.manager.impl;

import gemini.basic.dao.PersonDao;
import gemini.basic.dao.UserDao;
import gemini.basic.dao.UserStatusDao;
import gemini.basic.manager.UserManager;
import gemini.basic.model.Address;
import gemini.basic.model.Person;
import gemini.basic.model.User;
import gemini.basic.model.UserStatus;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserManagerImpl implements UserManager {

	private final Logger logger = LoggerFactory.getLogger(UserManager.class);

	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	private UserStatusDao userStatusDao;

	public void setUserStatusDao(UserStatusDao userStatusDao) {
		this.userStatusDao = userStatusDao;
	}

	private PersonDao personDao;

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public User getUserByName(String name) {

		logger.debug("============== get user from db by username!");

		User result = userDao.getByName(name);
		return result;
	}

	@Override
	public User save(User user) {
		logger.debug("============== save user");

		logger.debug("============== fill mandatory fields");
		user.setCreatedTime(new Date());
		user.setRetryLoginTimes(Short.parseShort("0"));
		user.setRetryPinTimes(Short.parseShort("0"));
		user.setValidFrom(new Date());

		logger.debug("============== set status to INI");
		user.setUserStatus(getFirstStatus());

		logger.debug("============== link with Person");
		Person ps = personDao.getById(user.getPerson().getId());
		user.setPerson(ps);

		logger.debug("============== update to db");
		User result = userDao.saveOrUpdate(user, true);

		return result;

	}

	private UserStatus getFirstStatus() {
		UserStatus result = userStatusDao.getAll().get(0);
		return result;
	}

	@Override
	public boolean savePassword(String username, String password,
			String newPassword) {
		User user = userDao.getByName(username);
		User result = null;
		if (user != null && user.getPassword().equals(password)) {
			user.setPassword(newPassword);
			result = userDao.saveOrUpdate(user, true);
		}
		if(result!=null){
			return true;
		}
		return false;
	}

	@Override
	public boolean saveEmail(String username, String password, String newEmail) {
		User user = userDao.getByName(username);
		User result = null;
		if (user != null && user.getPassword().equals(password)) {
			user.getPerson().setEmail(newEmail);
			result = userDao.saveOrUpdate(user, true);
		}
		if(result!=null){
			return true;
		}
		return false;
	}

	@Override
	public boolean savePhone(String username, String password, String newPhone) {
		User user = userDao.getByName(username);
		User result = null;
		if (user != null && user.getPassword().equals(password)) {
			user.getPerson().setPhoneNumber(newPhone);
			result = userDao.saveOrUpdate(user, true);
		}
		if(result!=null){
			return true;
		}
		return false;
	}

	@Override
	public boolean saveAddress(String username, String password,
			Address newAddress) {
		User user = userDao.getByName(username);
		User result = null;
		if (user != null && user.getPassword().equals(password)) {
			Address tempAddress = new Address();
			tempAddress = user.getPerson().getAddress();
			tempAddress.setAddressLine1(newAddress.getAddressLine1());
			tempAddress.setAddressLine2(newAddress.getAddressLine2());
			tempAddress.setCity(newAddress.getCity());
			tempAddress.setCountry(newAddress.getCountry());
			tempAddress.setDistrict(newAddress.getDistrict());

			user.getPerson().setAddress(tempAddress);
			result = userDao.saveOrUpdate(user, true);
		}
		if(result!=null){
			return true;
		}
		return false;
	}
}
